package com.kun.index.controller;


import com.kun.common.ResultDto;
import com.kun.index.controller.dto.AddBlogLabelDto;
import com.kun.index.entity.BlogLabel;
import com.kun.index.entity.Label;
import com.kun.index.service.impl.BlogLabelServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 博客_标签表 前端控制器
 * </p>
 *
 * @author kunkun
 * @since 2024-03-18
 */
@RestController
@RequestMapping("/index/blog-label")
public class BlogLabelController {

    @Autowired
    private BlogLabelServiceImpl blogLabelService;

    @PostMapping("/addBlogLabel")
    @ApiOperation("新增博客标签联系")
    public ResultDto addLabel(@RequestBody AddBlogLabelDto addBlogLabelDto) {

        for (Integer blogId : addBlogLabelDto.getBlogIds()) {
            BlogLabel blogLabel = new BlogLabel(addBlogLabelDto.getLabelId(), blogId);
            Integer id =blogLabelService.addBlogLabel(blogLabel);
            if (id==0||id==null){
                return ResultDto.fail("新增博客标签联系失败");
            }
        }
        return ResultDto.success();
    }

    @PostMapping("/updateBlogLabel")
    @ApiOperation("编辑博客标签联系")
    public ResultDto updateBlogLabel(@RequestBody AddBlogLabelDto addBlogLabelDto) {

        //删除该labelId下的所有博客标签联系
        blogLabelService.deleteBlogLabel(addBlogLabelDto.getLabelId());

        for (Integer blogId : addBlogLabelDto.getBlogIds()) {
            BlogLabel blogLabel = new BlogLabel(addBlogLabelDto.getLabelId(), blogId);
            Integer id =blogLabelService.addBlogLabel(blogLabel);
            if (id==0||id==null){
                return ResultDto.fail("编辑博客标签联系失败");
            }
        }
        return ResultDto.success();
    }

    @GetMapping("/queryByLabelId")
    @ApiOperation("通过labelId查询所有的blogLabel")
    public ResultDto queryByLabelId(@RequestParam("labelId") Integer labelId) {
        List<BlogLabel> blogLabels= blogLabelService.queryByLabelId(labelId);

        return ResultDto.success(blogLabels);
    }




}

